package cn.edu.hqu.cst.android.firstexperiment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Timer;
import java.util.TimerTask;

public class FrameActivity extends AppCompatActivity {
    int[] images = new int[]{
            R.drawable.p1,
            R.drawable.p2,
            R.drawable.p3,
            R.drawable.p4,
            R.drawable.p5,
    };
    int currentImg[] = {0, 1, 2, 3, 4};
    int i = 0, j = 1, k = 2, l = 3, m = 4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_frame);
        /////////////////////////////////////
        Timer timer = new Timer();
        final Handler handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case 1:
                        i++;
                        j++;
                        k++;
                        l++;
                        m++;
                        break;
                }
                super.handleMessage(msg);
                //imageView相关设置
                FrameLayout main = (FrameLayout) findViewById(R.id.f1);
                ImageView image1 = (ImageView) findViewById(R.id.imageView);
                ImageView image2 = (ImageView) findViewById(R.id.imageView2);
                ImageView image3 = (ImageView) findViewById(R.id.imageView3);
                ImageView image4 = (ImageView) findViewById(R.id.imageView4);
                ImageView image5 = (ImageView) findViewById(R.id.imageView5);
                image1.setImageResource(images[currentImg[i % 5]]);
                image2.setImageResource(images[currentImg[j % 5]]);
                image3.setImageResource(images[currentImg[k % 5]]);
                image4.setImageResource(images[currentImg[l % 5]]);
                image5.setImageResource(images[currentImg[m % 5]]);
            }
        };
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                Message message = new Message();
                message.what = 1;
                handler.sendMessage(message);
            }
        };
        timer.schedule(task,0,600);
        //返回键点击事件设置
        Button btn = (Button) findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener()

        {
            @Override
            public void onClick (View v){
                Intent i = new Intent(FrameActivity.this, MainActivity.class);
                startActivity(i);
            }
        });
    }
    //image5.setOnClickListener(new View.OnClickListener() {
    //@Override
    //public void onClick(View v){
    //if (currentImg >= 4) {
    //currentImg = -1;
    //}
    //image5.setImageResource(images[++currentImg]);
    //}
    //});
}

